"""
  Copyright (c) 2024 Northeastern University (China)
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
"""
import os
import re
import logging
import json


from ..extractors.extractor import Extractor
from ..extractors.dependency import Dependency
from ..utils.utils import read_js, read_json5
logging.basicConfig()
logger = logging.getLogger(__name__)


class DdsExtractor(Extractor):
    def __init__(self, target) -> None:
        super().__init__()
        self.target = target
        self.type = 'dds'

    def run_extractor(self):
        self.parse_dds()


    def parse_dds(self):
        contents = read_json5(self.target)
        if 'depends' not in contents:
            return
        for item in contents['depends']:
            dep_name = item
            version = contents['depends'][item]
            ## get unified name and parse version operator
            dep = Dependency(dep_name, version)
            dep.add_evidence(self.type, self.target, 'High')
            self.add_dependency(dep)